<?php namespace Admin\Controller;

use Hdphp\Controller\Controller;

//货品列表控制器
class shopGoodsListController extends CommonController {
	private $model;
	public function __init() {
		parent::__init();
		//实例化shopGoodsList表
		$this->model = new \Common\model\shopGoodsList;
	}

	//默认主页
	public function index() {
		//接收get过来的gid
		$gid = Q('get.gid',0,'intval');
		//分配,编辑要用
		View::with('gid',$gid);
		//1.商品属性表和类型属性表关联
		//先实例化商品属性表
		$goodsAttrModel = new \Common\Model\shopGoodsAttr;
		//关联
		$data = $goodsAttrModel->join('shop_type_arrt','taid','=','type_taid')->where("goods_gid={$gid} AND tatype = 1") ->get();
		//循环出来当前gid对应的商品属性
		$datas = array();
		foreach ($data as $k => $v) {
			$datas[$v['gaid']] = $v['gavalue'];
		}
		p($datas);
		//定义一个空数组
		$arr = array();
		foreach ($data as $k => $v) {
			//把键值当做键名,$v(键值)里面有和键名一样的属性就把数组添加进去
			$arr[$v['taname']][] = $v;

		}
		View::with('arrData',$arr);
		//2.显示对应商品表(gid)数据
		$goodsListData = $this->model->where("goods_list_gid={$gid}")->get();
		foreach ($goodsListData as $k => $v) {
			//把combine字符串拆分成数组
			$arrs = explode(',', $v['combine']);
			//定义一个新数组
			$newDatas = array();
			//循环被拆分的那个数组,
			foreach ($arrs as $value) {
				$newDatas[] = $datas[$value];
			}
			$goodsListData[$k]['combine'] = $newDatas;
		}
		View::with('goodsListData',$goodsListData);
		//默认模版
		View::make();
	}

	//添加货品
	public function add() {
		if (IS_POST) {
			//组合内容不能为空
			foreach ($_POST['combine'] as $v) {
				if(!$v){
					View::error('内容不能为空');
				}
			}
			if (!$this->model->store()) View::error($this->model->getError());
			View::success('添加成功');
		}
		//载入模板
		View::make();
	}

	//删除货品
	public function del() {
		//获得要删除数据的下标
		$glid = Q('get.glid',0,'intval');
		$this->model->where("glid={$glid}")->delete();
		//成功提示
		View::success('删除成功');
	}

	//编辑货品
	public function edit() {
		//3.用户提交
		$gid = Q('get.gid',0,'intval');
		p($gid);
		if (IS_POST) {
			//组合内容不能为空
			foreach ($_POST['combine'] as $v) {
				if(!$v){
					View::error('内容不能为空');
				}
			}
			if (!$this->model->edit())
				View::error($this->model->getError());
				View::success('编辑成功',U('index',array('gid'=>$gid)));
		}
		//1.获取对应可选规格
		//接收编辑传递过来的id
		$glid = Q('get.glid',0,'intval');
		//获得商品属性表
		$goodsAttrModel = new \Common\Model\shopGoodsAttr;
		$datas = $goodsAttrModel->join('shop_type_arrt','taid','=','type_taid')->where("goods_gid={$gid} AND tatype=1")->get();
		//定义一个空数组
		$arrData = array();
		foreach ($datas as $k => $v) {
			$arrData[$v['taname']][] = $v;
		}
		View::with('arrData',$arrData);

		//2.获取旧数据
		//获得货品列表
		$oldData = $this->model->where("glid={$glid}")->find();
		$arr = explode(',',$oldData['combine']);
		$oldData['combine'] = $arr;
		View::with('oldData',$oldData);
		//默认模版
		View::make();
	}

	public function check(){
		$gid = Q('post.');
		//实例化货品列表模型
		//把货品列表里面的combine字段里面数据组合成字符串
		$specs = implode(',',$gid['spec']);
		//截取数据
		$data = $this->model->where("combine",$specs)->find();
		if($data){
			echo json_encode(0);exit;
		}else{
			echo json_encode(1);exit;
		}
	}

}
